<?php

class HomeController extends BaseController {


	function index()
	{
		$token=Input::get("access");

		if($token==Config::get('appconfig.accessToken')){
			return View::make('upload');
		} else {
			return View::make('hello');
		}
		
	}

	function upload()
	{

		$file = Input::file('file');

		if($file===null){
			return Redirect::to('csv?access=test')->with("error","no upload file");
		}
		

		$destinationPath = Config::get("appconfig.uploadFolder");

		$filename='SalesHistory.csv';
		$upload_success = $file->move($destinationPath, $filename);

		if( $upload_success ) {
			(new Record)->updateNumOfUse();
			$converter=new Converter;
			$converter->setSourceFile($filename);
			$converter->download();
		} else {
			return Response::json('error', 400);
		}

	}

	function numOfUse()
	{
		$num = (new Record)->getNumOfUse();
		return Response::json($num);
	}
}
